<template>
    <div class="login_out">
      <div class="login_in">
        <form :model="form1">
          <input class="phone" v-model="form1.phone" placeholder="请输入手机号">
          <div class="ver_out">
            <input class="verification" v-model="form1.verification" placeholder="请输入验证码" maxlength="4">
            <!--<div class="get_v" @click="send">获取验证码</div>-->
            <el-row>
              <el-button class="get_v" @click="send" :disabled='tag'>{{btnCont}}</el-button>
            </el-row>
          </div>
          <div class="ver_sub" @click="post">立即拿钱</div>
          <div class="ver_tips">立即拿钱表示同意《用户服务协议》</div>
        </form>
      </div>
    </div>
</template>

<script>
  import {urlList} from '../assets/urlList.js'//引入接口地址文件
  let phoneReg=/^[1][3,4,5,7,8][0-9]{9}$/;//手机号正则表达式
  //获取地址栏信息
  function GetQueryString(name)
  {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = decodeURI(window.location.search.substr(1)).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
  }

  let channelNum = GetQueryString("channelNum")
  if(!channelNum){
    channelNum = 0
  }
  function getCookie(cname)
  {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
    {
      var c = ca[i].trim();
      if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
  }
  let auth = getCookie('USER_P2P_H5_COOKIE');//获取cookie

  let params = new URLSearchParams();
  let params2 = new URLSearchParams();
  params.append('mobilePhone','')
  params.append('vCode','')
  params.append('auth',auth)
  params.append('fromChannel',111)
  params2.append('mobilePhone','')
  params2.append('auth',auth)
  var phone = ''
    export default {
      name: "login",
      data() {
        return{
          form1:{
            phone:'',
            verification:''
          },
          btnCont:'获取验证码',
          tag:false,
        }
      },
      methods:{
        testCookie() {
          if(auth!='') {
            window.location.href='index.html?channelNum='+channelNum
          }
        },
        post() {
          let _this = this;
          this.$ajax({
            method:'post',
            url:'/api/customer/login.html?channelNum='+channelNum,
            data:params
          }).then(function(res) {
            if(res.data.success == 1000){
               window.location.href='index.html?channelNum='+channelNum
//              window.history.back(-1);
              console.log(res);
//              设置cookie失效期为30天
              var Days = 30;
              var exp = new Date();
              exp.setTime(exp.getTime() + Days*24*60*60*1000);
              document.cookie = 'USER_P2P_H5_COOKIE' + "="+ res.data.cookie + ";expires=" + exp.toGMTString();
              // localStorage.setItem('userInfo',res.data.cookie)
            }else if(res.data.success == 2000){
              alert(res.data.msg)
            }else if(res.data.success == 3000){
              alert('系统出错')
            }
          }).catch(function(res) {
            console.log('失败')
            console.log(res)
          })
        },
        send() {
          let _this = this
          if(!phoneReg.test(this.form1.phone)){
            this.$alert('手机号输入不正确','提示',{
              confirmButtonText:'确定',
              type:'warning',
              center:true
            })
            console.log(typeof _this.form1.phone)
          }else{
            _this.tag = true
            _this.btnCont = '60s'
            let num = 60
            setInterval(function() {
              num--
              if(num>=0){
                _this.btnCont = num+'s'
              }else{
                _this.btnCont = '获取验证码'
                _this.tag = false
              }
            },1000);
            console.log(this.form1.phone)
            this.$ajax({
              method:'post',
              url:'/api/sms/send.html?channelNum='+channelNum,
              data:params2
            }).then(function(res) {
            }).catch(function(res) {
            })
          }
        }
      },
      watch:{
        form1:{
          handler(newValue, oldValue){
            params.set('mobilePhone',newValue.phone)
            params2.set('mobilePhone',newValue.phone)
            params.set('vCode',newValue.verification)
          },
          deep:true
        }
      },
      mounted() {
        // document.cookie =name + "=haomowei"
        this.testCookie()
      }
    }
</script>

<style scoped>
  .el-input__inner{
    border: 1px solid #dcdfe6 !important;
  }
  .login_out{
    position: relative;
    top:30%;
  }
</style>
<style src="../assets/reset.css"></style>
<style src="../assets/style.css"></style>
